package com.gwd.task.controller;

import com.gwd.task.group.GroupInsert;
import com.gwd.task.group.GroupUpdate;
import com.gwd.task.model.CodeSubmitRecordDTO;
import com.gwd.task.model.PageUtils;
import com.gwd.task.model.TaskDTO;
import com.gwd.task.model.TaskProcessDTO;
import com.gwd.task.services.TaskProcessServer;
import com.gwd.task.services.TaskServer;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhiyongming
 * @date 2020/11/9 13:41
 */
@RestController
@RequestMapping(value = "/v1/task")
@SuppressWarnings("all")
public class TaskController {
    @Resource
    private TaskServer taskServer;
    @Resource
    private TaskProcessServer taskProcessServer;

    /**
     * 任务分页查询
     * @Return PageUtils
     * @Params TaskDTO, PageUtils
     */
    @GetMapping(value = "/findByPage")
    public PageUtils findByPage(@Validated TaskDTO dto, PageUtils page) {
        return taskServer.findByPage(dto, page);
    }

    @GetMapping(value = "/findById/{taskId}")
    public TaskDTO findById(@PathVariable(name = "taskId") Long taskId) {
        return taskServer.findById(taskId);
    }

    @PostMapping(value = "/add")
    public void add(@RequestBody @Validated({GroupInsert.class}) TaskDTO dto, @RequestAttribute("userId") Long id) {
        taskServer.save(dto, id);
    }

    @PutMapping(value = "/edit")
    public List<TaskProcessDTO> edit(@RequestBody @Validated({GroupUpdate.class}) TaskDTO dto, @RequestAttribute("userId") Long id) {
        taskServer.save(dto, id);
        return taskProcessServer.findByTaskId(dto.getTaskId());
    }

    @PutMapping(value = "/start/{id}")
    public TaskDTO start(@PathVariable(value = "id") Long taskId, @RequestAttribute("userId") Long userId ) {
        return taskServer.start(taskId, userId);
    }

    @PutMapping(value = "/restart/{id}")
    public List<TaskProcessDTO> reStart(@PathVariable(value = "id") Long taskId, @RequestAttribute("userId") Long userId ) {
        taskServer.reStart(taskId, userId);
        return taskProcessServer.findByTaskId(taskId);
    }

    @PutMapping(value = "/submit/{id}")
    public void submit(@PathVariable(value = "id") Long id, @RequestBody CodeSubmitRecordDTO csrDTO, @RequestAttribute("userId") Long userId) {
        taskServer.submit(id, userId, csrDTO);
    }

    @PutMapping(value = "/test/{id}")
    public List<TaskProcessDTO> test(@PathVariable(value = "id") Long taskId, @RequestAttribute("userId") Long userId ) {
        taskServer.test(taskId, userId);
        return taskProcessServer.findByTaskId(taskId);
    }

    @PutMapping(value = "/finish/{id}")
    public List<TaskProcessDTO> finish(@PathVariable(value = "id") Long taskId, @RequestAttribute("userId") Long userId ) {
        taskServer.finish(taskId, userId);
        return taskProcessServer.findByTaskId(taskId);
    }

    @PutMapping(value = "/cancel/{id}")
    public List<TaskProcessDTO> cancel(@PathVariable(value = "id") Long taskId, @RequestAttribute("userId") Long userId ) {
        taskServer.cancel(taskId, userId);
        return taskProcessServer.findByTaskId(taskId);
    }

    @PutMapping(value = "/block/{id}")
    public List<TaskProcessDTO> block(@PathVariable(value = "id") Long taskId, @RequestParam(value = "describe") String describe, @RequestAttribute("userId") Long userId ) {
        taskServer.block(taskId, userId, describe);
        return taskProcessServer.findByTaskId(taskId);
    }

    @DeleteMapping(value = "/remove/{id}")
    public void remove(@PathVariable(value = "id") Long id) {
        taskServer.remove(id);
    }
}
